package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {

	gin.SetMode(gin.ReleaseMode)
	r := gin.New()
	r.GET("/", func(c *gin.Context) {
		i := c.Query("i")
		fmt.Printf("=====================start=%s==================\n", i)
		time.Sleep(2 * time.Second)
		fmt.Printf("=====================end=%s==================\n", i)
		c.JSON(200, gin.H{"message": "hello world"})
	})
	ser := &http.Server{
		Addr:    ":8080",
		Handler: r,
	}

	go runServer(ser)

	quit := make(chan os.Signal)
	// kill (no param) default send syscall.SIGTERM
	// kill -2 is syscall.SIGINT
	// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit

	fmt.Println("dsssssssssss")
}

func runServer(ser *http.Server) {
	if err := ser.ListenAndServe(); err != nil && err != http.ErrServerClosed {
		log.Fatalf("listen: %s\n", err)
	}
}
